Dart List operator []=
Syntax & Examples


Syntax of List.operator []=

The syntax of List.operator []= operator is:

operator []=(int index, E value) → void

This operator []= operator of List sets the value at the given index in the list to value.

Parameters

ParameterOptional/RequiredDescription
indexrequiredthe index at which to set the value in the list
valuerequiredthe value to set at the specified index


✐ Examples

1 Set element at index 2 to 10 in a list of numbers

In this example,

  1. We create a list numbers containing integers.
  2. We use the []= operator to set the element at index 2 to the value 10.
  3. We print the modified numbers list to standard output.

Dart Program

void main() {
  List<int> numbers = [1, 2, 3, 4, 5];
  numbers[2] = 10;
  print(numbers);
}

Output

[1, 2, 10, 4, 5]

2 Set element at index 1 to 'd' in a list of characters

In this example,

  1. We create a list characters containing characters.
  2. We use the []= operator to set the element at index 1 to the character 'd'.
  3. We print the modified characters list to standard output.

Dart Program

void main() {
  List<String> characters = ['a', 'b', 'c'];
  characters[1] = 'd';
  print(characters);
}

Output

[a, d, c]

3 Set element at index 0 to 'Eve' in a list of names

In this example,

  1. We create a list names containing strings.
  2. We use the []= operator to set the element at index 0 to the string 'Eve'.
  3. We print the modified names list to standard output.

Dart Program

void main() {
  List<String> names = ['Alice', 'Bob', 'Charlie'];
  names[0] = 'Eve';
  print(names);
}

Output

[Eve, Bob, Charlie]

Summary

In this Dart tutorial, we learned about operator []= operator of List: the syntax and few working examples with output and detailed explanation for each example.